#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <linux/input.h>

#define DEV_BUZZER    "/dev/event2"

int dev_buzzer_fd;

int main(int argc, char **argv)
{
	struct input_event event;
	
	if(argc < 2)
	{
		printf("usage:%s <beep_freq 0~1000>\n",argv[0]);
		return -1;
	}
	
	if((dev_buzzer_fd = open (DEV_BUZZER,O_RDWR | O_NONBLOCK)) < 0)
	{
		printf("Cann't open file /dev/beep\n");
		exit(-1);
	}
	printf("pthread_buzzer is ok\n);
	
	while(1)
	{
		pthread_mutex_lock(&mutex_buzzer);
		pthread_cond_wait(&cond_buzzer,&mutex_buzzer);
		printf("pthread_buzzer is wake up\n");
		
		if(dev_buzzer_mask)
		{
		// ioctl(dev_buzzer_fd,BEEP_ON);
			event.type = EV_SND;
			event.code = SND_TONE;
			event.value = 100;
			write(dev_buzzer_fd,&event,sizeof(struct input_event));
			printf("**buzzer on\n");
		}else{
			//ioctl(dev_buzzer_fd,BEEP_OFF);
			event.type = EV_SND;
			event.code = SND_TONE;
			event.value = 0;
			write(dev_buzzer_fd, &event, sizeof(struct input_event));
			printf("**buzzer off\n");
		}
		pthread_mutex_unlock (&mutex_buzzer);
	}
	return 0;
}